package emergence.resource;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import emergence.resource.inputfile.GraphicConfigFile;
import emergence.resource.inputfile.LevelConfigFile;
import emergence.resource.inputfile.LevelMapConfigFile;
import util.Logger;

/**
 * A class representing "directory" files for resources for an EmergenceEngine
 * game
 *
 * @author Hortator
 *
 */
public class ResourceInputFile {
    
    private static final Logger logger = Logger.getDefaultLogger();

    private String filePath;
    private String[] contents;

    public ResourceInputFile(String path) {
        this.filePath = path;
    }

    public ResourceInputFile(File file) {
        this.filePath = file.getAbsolutePath();
    }

    public void readData() {
        inputData();
        processData();
    }

    /**
     * Reads the data from the file
     */
    private void inputData() {
        File inputFile = new File(filePath);

        try {
            Scanner scan;
            try (Scanner initialScan = new Scanner(inputFile)) {
                scan = initialScan.useDelimiter("\\Z");
                contents = scan.next().split("\n");
            }
            scan.close();
        }
        catch (FileNotFoundException e) {
            logger.log(e);
            e.printStackTrace();
        }
        
        Resources.setResIniFilePath(filePath);
    }

    /**
     * Processes the data and assembles resources based on what is in the
     * provided File
     */
    private void processData() {
        String currentHeader = "";
        for (String line : contents) {
            line = cleanComments(line).trim();

            if (line.startsWith("[") & line.endsWith("]")) {
                currentHeader = line.substring(1, line.length() - 1).toUpperCase();
            }

            switch (currentHeader) {
                case "GRAPHICS":
                    if (line.toUpperCase().contains("[GRAPHICS]") || line.equals("")) {
                        break;
                    }
                    String cfgPath = getFolderFrom(filePath) + line;
                    GraphicConfigFile grcf = new GraphicConfigFile(cfgPath);
                    grcf.readData();
                    Resources.addGfxConfig(line);
                    break;
                case "LEVELMAP":
                    if (line.toUpperCase().contains("[LEVELMAP]") || line.equals("")) {
                        break;
                    }
                    LevelMapConfigFile lvlmpcfg = new LevelMapConfigFile(getFolderFrom(filePath) + line);
                    lvlmpcfg.readData();
                    break;
                case "LEVEL":
                    if (line.toUpperCase().contains("[LEVEL]") || line.equals("")) {
                        break;
                    }
                    LevelConfigFile lvlCfg = new LevelConfigFile(getFolderFrom(filePath) + line);
                    lvlCfg.readData();
                    break;
                default:
                    break;
            }
        }
    }

    public static String getFolderFrom(String path) {
        int indexOfBackSlash = path.lastIndexOf('\\');
        int indexOfForwardSlash = path.lastIndexOf('/');
        int indexOfSlash = Math.max(indexOfForwardSlash, indexOfBackSlash);
        return path.substring(0, indexOfSlash);
    }

    /**
     * Cleans comments (denoted by '//') from a String
     *
     * @param s The String to have comments cleaned from
     *
     * @return Returns the String that has been cleaned of comments
     */
    private String cleanComments(String s) {
        if (!s.contains("//")) {
            return s;
        }
        else {
            int index = s.indexOf("//");
            return s.substring(0, index);
        }
    }
    
}
